//
//  EditView.m
//  StationManager
//
//  Created by 张佳强 on 18/5/16.
//  Copyright © 2018年 Helong. All rights reserved.
//

#import "EditView.h"
#import <Masonry.h>
#import <QuartzCore/QuartzCore.h>

#define BLUECOLOR [UIColor colorWithRed:34/255.0 green:150/255.0 blue:243/255.0 alpha:1.0]
@implementation EditView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"编辑导航位置";
        titleLabel.textColor = BLUECOLOR;
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.right.equalTo(@0);
            make.height.mas_equalTo(40);
        }];
        
        _stationNameLabel = [[UILabel alloc] init];
        _stationNameLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_stationNameLabel];
        
        [_stationNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@5);
            make.top.equalTo(titleLabel.mas_bottom);
            make.right.equalTo(@-5);
            make.height.mas_equalTo(30);
        }];
        
        _stationNumLabel = [[UILabel alloc] init];
        _stationNumLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_stationNumLabel];
        
        [_stationNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@5);
            make.top.equalTo(_stationNameLabel.mas_bottom);
            make.right.equalTo(@-5);
            make.height.mas_equalTo(30);
        }];
        
        _latitudeLabel = [[UILabel alloc] init];
        _latitudeLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_latitudeLabel];
        
        [_latitudeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@5);
            make.top.equalTo(_stationNumLabel.mas_bottom);
            make.right.equalTo(@-5);
            make.height.mas_equalTo(30);
        }];
        
        _longitudeLabel = [[UILabel alloc] init];
        _longitudeLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_longitudeLabel];
        
        [_longitudeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@5);
            make.top.equalTo(_latitudeLabel.mas_bottom);
            make.right.equalTo(@-5);
            make.height.mas_equalTo(30);
        }];
        
        _locationLabel = [[UILabel alloc] init];
        _locationLabel.font = [UIFont systemFontOfSize:13];
        _locationLabel.numberOfLines = 0;
        [self addSubview:_locationLabel];
        
        [_locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@5);
            make.top.equalTo(_longitudeLabel.mas_bottom);
            make.right.equalTo(@-5);
            make.height.mas_equalTo(35);
        }];
        
        _naviLabel = [[UILabel alloc] init];
        _naviLabel.text = @"导航位置：";
        _naviLabel.font = [UIFont systemFontOfSize:13];
        
        [self addSubview:_naviLabel];
        
        [_naviLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@5);
            make.top.equalTo(_locationLabel.mas_bottom);
            make.width.mas_equalTo(70);
            make.height.mas_equalTo(30);
        }];
        
        _naviTF = [[UITextField alloc] init];
        _naviTF.font = [UIFont systemFontOfSize:13];
        _naviTF.layer.borderColor= [UIColor clearColor].CGColor;
        _naviTF.delegate = self;
        _naviTF.layer.borderWidth= 1.0f;
        [self addSubview:_naviTF];
        
        [_naviTF mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_naviLabel.mas_right);
            make.top.equalTo(_locationLabel.mas_bottom);
            make.right.equalTo(@-5);
            make.height.mas_equalTo(30);
        }];
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = BLUECOLOR;
        [self addSubview:line];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_naviTF);
            make.top.equalTo(_naviTF.mas_bottom);
            make.right.equalTo(@-5);
            make.height.mas_equalTo(2);
        }];

        _saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:BLUECOLOR forState:UIControlStateNormal];
        _saveButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_saveButton addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_saveButton];
        
        [_saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(@-10);
            make.bottom.equalTo(@-10);
            make.width.mas_equalTo(40);
            make.height.mas_equalTo(30);
        }];
        
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:BLUECOLOR forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelButton];
        
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(_saveButton.mas_left).offset(-10);
            make.bottom.equalTo(@-10);
            make.width.mas_equalTo(40);
            make.height.mas_equalTo(30);
        }];
        
    }
    return self;
}

-(void)saveClick{
    
    [self.editDelegate saveButtonClick];
}

-(void)cancelClick{
    
    [self.editDelegate cancelButtonClick];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(40, SCREEN_WIDTH/3-80, SCREEN_WIDTH-80, 270);

    }];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(40, SCREEN_WIDTH/3, SCREEN_WIDTH-80, 270);
        
    }];
}
@end
